package game.module.mj.processor;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.mj.bean.MjRoom;
import game.module.mj.logic.MjDataManager;
import game.module.offline.bean.PlayerOfflineCache;
import game.module.offline.logic.PlayerOfflineManager;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageMj.C2SMjBackRoomHall;
import lua.LuaMessageLogin.S2ErrorCode;
import lua.LuaMessageMj.MJMemberInfo;
import lua.LuaMessageMj.S2CMJJoinRoom;
import lua.LuaMessageMj.S2CMjBackRoomHall;

@MsgCodeAnn(msgcode = C2SMjBackRoomHall.id,accessLimit = 500)
public class MjBackRoomProcessor extends PlayingRoleMsgProcessor{

	private static Logger logger =  LoggerFactory.getLogger(MjBackRoomProcessor.class);
	
	@Override
	public void process(PlayingRole playingRole, RequestMessageRaw requestMessage) throws Exception {
		int playerId = playingRole.getId();
		logger.info("mj back to hall room,playeyId={}", playerId);
		// 是否在房间中 
		boolean isInRoom = MjDataManager.getInstance().isPlayerInRoom(playerId);
		if (!isInRoom) {
			S2ErrorCode respMsg = new S2ErrorCode(106);
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;
		}
		//房间是否存在
		MjRoom ddzRoom = MjDataManager.getInstance().getMjRoomByPlayerId(playerId);
		if (ddzRoom == null) {
			S2ErrorCode respMsg = new S2ErrorCode(104);
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;
		}
		//ret 
		String room_id = ddzRoom.getUuid();
		S2CMjBackRoomHall respMsg = new S2CMjBackRoomHall();
		respMsg.hall_info = new S2CMJJoinRoom();
		respMsg.hall_info.room_id = room_id;
		respMsg.hall_info.round_count = ddzRoom.getRoundCount();
		respMsg.hall_info.dapai = ddzRoom.getDapai();
		respMsg.hall_info.typehu = ddzRoom.getTypehu();
		respMsg.hall_info.cost_type = ddzRoom.getCost_type();
		respMsg.hall_info.ready = ddzRoom.getReadyState();
		respMsg.hall_info.members = new MJMemberInfo[ddzRoom.getPlayerList().size()];
		respMsg.hall_info.prevent_cheat = ddzRoom.getPrevent_cheat();
		List<Integer> playerIdList = ddzRoom.getPlayerList();
		int i = 0;
		for(Integer aPlayerId : playerIdList) {
			PlayerOfflineCache poc = PlayerOfflineManager.getInstance().getPlayerOfflineCache(aPlayerId);
			respMsg.hall_info.members[i++] = new MJMemberInfo(aPlayerId, poc.getHeadimg(), poc.getLevel(),
					poc.getSex().shortValue(), poc.getName());
		}
		playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		
	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
		
	}

}
